package com.mzy.multithreading.collect;

import java.util.UUID;
import java.util.concurrent.CopyOnWriteArraySet;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author mzy
 * @Date 2022/8/8 7:30
 * @Version 1.0
 */
public class HashSetThreadSafe {
    public static void main(String[] args) {
//        HashSet<String> set = new HashSet<>();

        // 解决方案 CopyOnWriteArrayList 解决  读时写技术， 写时复制一份原来的副本写。写完覆盖回去   **常用**
        CopyOnWriteArraySet<String> set = new CopyOnWriteArraySet<>();
        for (int i = 0; i < 30; i++) {
            new Thread(() -> {
                // 向集合中添加内容
                set.add(UUID.randomUUID().toString().substring(0, 8));
                // 从集合中获取内容
                System.out.println(set);
            }, String.valueOf(i)).start();
        }
    }
}
